<?php
/**
* Description of BlogPing
*
* @version  1.0
* @author Daniel Eliasson - joomla at stilero.com
* @copyright  (C) 2012-aug-28 Stilero Webdesign http://www.stilero.com
* @category Custom Form field
* @license    GPLv2
*
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*
* This file is part of appirater.
*
* BlogPing is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BlogPing is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BlogPing.  If not, see <http://www.gnu.org/licenses/>.
*
*/
 
// no direct access
defined('_JEXEC') or die('Restricted access');
class appirater{
    private static function nextReminder($date = ""){
        $twoWeeksFromNow = time() + (14 * 24 * 60 * 60);
        if($date == "" || $date=="0000000000"){
            return $twoWeeksFromNow;
        }else{
            return $date;
        }
    }
    
    private static function hiddenField($id, $name, $value){
        $value = self::nextReminder($value);
        $htmlCode = '<input id="'.$id.'" name="'.$name.'" type="hidden" value="'.$value.'" />';
        return $htmlCode;
    }
    
    private static function optionField($id, $name, $value, $isJ15=false){
        $reminder = self::nextReminder($value);
        $firstChecked = $value == "0000000000" ? '' : ' checked="checked"';
        $secondChecked = $value == "0000000000" ? ' checked="checked"' : '';
        $value = $value == "0000000000" ? $reminder : $value;
        
        $htmlCode = 
                '<input type="radio" id="'.$id.'0" name="'.$name.'" value="'.$value.'"'.$firstChecked.'>'.
                '<label for="'.$id.'0">Remind me later</label>'.
                '<input type="radio" id="'.$id.'1" name="'.$name.'" value="0000000000"'.$secondChecked.'>'.
                '<label for="'.$id.'0">Stop reminding me</label>';
        if(!$isJ15){
            $htmlCode = '<fieldset id="'.$id.'" class="radio">'.$htmlCode.'</fieldset>';
        }
        return $htmlCode;
    }
    
    static function appiraterField($id, $name, $value, $isJ15=false){
        $currentDate = time();
        $nextReminder = $value;
        if($value == ""){
            return self::hiddenField($id, $name, $value);
        }
        if($currentDate > $nextReminder ){
            return self::optionField($id, $name, $value, $isJ15);
        }else{
            return self::hiddenField($id, $name, $value);
        }
    }
}
if(version_compare(JVERSION, '1.6.0', '<')){
    /**
    * @since J1.5
    */
    class JElementAppirater extends JElement{

        function fetchElement($name, $value, &$node, $control_name){
            $id = $control_name.$name;
            $name = $control_name.'['.$name.']';
            $value = $value;
            return appirater::appiraterField($id, $name, $value, TRUE);
        }
        function fetchTooltip ( $label, $description, &$xmlElement, $control_name='', $name=''){
            $output = '<label id="'.$control_name.$name.'-lbl" for="'.$control_name.$name.'"';
            if ($description) {
                    $output .= ' class="hasTip" title="'.JText::_($label).'::'.JText::_($description).'">';
            } else {
                    $output .= '>';
            }
            $output .= JText::_( $label ).'</label>';

            return $output;        
        }
    }//End Class J1.5
}else{
    /**
    * @since J1.6
    */
    class JFormFieldAppirater extends JFormField {
        protected $type = 'appirater';

        protected function getInput(){
            $id = $this->id;
            $name = $this->name;
            $value = $this->value;
            return appirater::appiraterField($id, $name, $value);
        }
        
        protected function getLabel(){
            $toolTip = JText::_($this->element['description']);
            $text = JText::_($this->element['label']);
            $labelHTML = '<label id="'.$this->id.'-lbl" for="'.$this->id.'" class="hasTip" title="'.$text.'::'.$toolTip.'">'.$text.'</label>';
            return $labelHTML;
        }
        
    }//End Class
}